<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonModule for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace User\Controller;

use User\Model\Entity\Settings;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class ModuleSettingsController extends AbstractActionController {
	/**
     * @var UserModel
     */
	protected $moduleSettingsModel;
	
	public function getModuleSettingsModel() {
		if(!$this->moduleSettingsModel) {
			$sm = $this->getServiceLocator();
			$this->moduleSettingsModel = $sm->get('User\Model\ModuleSettingsModel');
		}
		return $this->moduleSettingsModel;
	}

	public function settingsAction() {
		$id = $this->params()->fromRoute('id');
		$form = new \User\Form\ModuleSettingsForm();
		$moduleSettings = $this->getModuleSettingsModel()->getModuleSettings($id);
		$form->bind($moduleSettings);
		$request = $this->getRequest();
		
		if($request->isPost()) {
			$form->setData($request->getPost());

			if($form->isValid()) {
				$this->getModuleSettingsModel()->saveModuleSettings($moduleSettings);
				$this->flashmessenger()->addSuccessMessage("The update was successful!");
				return $this->redirect()->toRoute('user/modulesettings');
			}
		}

		return array(
         	'form' => $form,
		);
	}
}